package org.hsc.proxy;
/*
 * HttpProxyHandler.java
 *
 * Created on December 22, 2007
 *
 * Name:        Moty Michaely   |   Alon Halfon
 * ID:          043413087       |   300545969
 */

import java.util.Date;

/**
 * An http context.
 *
 * @author Moty
 */
public class HttpContext {
    /**
     * Holds the Http Response for the current request.
     */
    private volatile HttpResponse response = null;
    
    /**
     * Holds the current request.
     */
    private volatile HttpRequest request = null;
    
    /**
     * Holds the Http Proxy Server that initiated the current request handler.
     */
    private volatile HttpProxyServer proxyServer = null;
    
    /**
     * Holds the current request timestamp.
     */
    private volatile Date timestamp = null;

    /**
     * Holds the cache result (if the request was fetched from the cache or not).
     */
    private volatile Cache.CacheResult cacheResult = Cache.CacheResult.NA;
    
    /**
     * Holds the server connection type.
     */
    private volatile HttpProxyServer.ConnectionType serverConnectionType = HttpProxyServer.ConnectionType.New; 
    
    /**
     * Holds the client connection type.
     */
    private volatile HttpProxyServer.ConnectionType clientConnectionType = HttpProxyServer.ConnectionType.New;
    
    /**
     * Constructs a new HttpContext for the specified http request.
     * 
     * @param request
     */
    public HttpContext(HttpRequest request, 
            HttpProxyServer.ConnectionType clientConnectionType, 
            HttpProxyServer proxyServer) {
        this.timestamp = new Date();
        this.request = request;
        this.proxyServer = proxyServer;
        this.clientConnectionType = clientConnectionType;
    }
    
    /**
     * Gets the Http Response for the current request.
     */
    public HttpResponse getResponse() {
        return response;
    }
    
    /**
     * Sets the Http Response for the current request.
     */
    public synchronized  void setResponse(HttpResponse response) {
        this.response = response;
    }

    /**
     * Gets the current request.
     */
    public HttpRequest getRequest() {
        return request;
    }

    /**
     * Gets the Http Proxy Server that initiated the current request handler.
     */
    public HttpProxyServer getProxyServer() {
        return proxyServer;
    }

    /**
     * Gets the current request timestamp.
     */
    public Date getTimestamp() {
        return timestamp;
    }

    /**
     * Gets the cache result (if the request was fetched from the cache or not).
     */
    public Cache.CacheResult getCacheResult() {
        return cacheResult;
    }

    /**
     * Sets the cache result (if the request was fetched from the cache or not).
     */
    public synchronized void setCacheResult(Cache.CacheResult cacheResult) {
        this.cacheResult = cacheResult;
    }

    /**
     * Gets the server connection type.
     */
    public HttpProxyServer.ConnectionType getServerConnectionType() {
        return serverConnectionType;
    }

    /**
     * Sets the server connection type.
     */
    public void setServerConnectionType(HttpProxyServer.ConnectionType serverConnectionType) {
        this.serverConnectionType = serverConnectionType;
    }

    /**
     * Gets the client connection type.
     */
    public HttpProxyServer.ConnectionType getClientConnectionType() {
        return clientConnectionType;
    }

    /**
     * Sets the client connection type.
     */
    public void setClientConnectionType(HttpProxyServer.ConnectionType clientConnectionType) {
        this.clientConnectionType = clientConnectionType;
    }
}
